package miguel.utils;

import miguel.objects.SerieExtendida;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SeriesLySQLiteHelper extends SQLiteOpenHelper {

	//Sentencia SQL para crear la tabla de Usuarios
	String sqlCreateTablaUsuario = "CREATE TABLE IF NOT EXISTS `Serie` (`idSerie` int(11), `title` text NOT NULL, " +
		  "`seasons` int(11) NOT NULL,`episodes` int(11) NOT NULL,`poster` text NOT NULL, `thumb` text NOT NULL,`small_thumb` text NOT NULL, " +
		  "`status` text NOT NULL,`synopsis` text NOT NULL,`seriesly_score` int(11) NOT NULL,`participants_score` int(11) NOT NULL," +
		  "`esNueva` int(1) NOT NULL, PRIMARY KEY (`idSerie`));";

	String sqlCreateTablaSerie = "CREATE TABLE IF NOT EXISTS `Temporada` (`idTemporada` int(11),`title` text NOT NULL," +
		"`idSerie` int(11) NOT NULL, `esNueva` int(1) NOT NULL, PRIMARY KEY (`idTemporada`));";
	
	String sqlCreateEpisodio = "CREATE TABLE IF NOT EXISTS `Episodio` (`idEpisodio` int(11),`title` text NOT NULL," +
		"`season` text NOT NULL,`viewed` int(11) NOT NULL,`idTemporada` int(11) NOT NULL, " +
		"`esNueva` int(1) NOT NULL, PRIMARY KEY (`idEpisodio`));";
	
	String sqlCreateLink = "CREATE TABLE IF NOT EXISTS `Link` (`idLink` int(11) ,`language` text NOT NULL," +
		"`subtitles` text NOT NULL,`hd` int(11) NOT NULL,`url_cineraculo` text NOT NULL,`idEpisodio` text NOT NULL, " +
		"`esNueva` int(1) NOT NULL, PRIMARY KEY (`idLink`));";
	
	public SeriesLySQLiteHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {		
		//Se ejecutan la sentencias SQL de creación de las tablas
        db.execSQL(sqlCreateTablaUsuario);
        db.execSQL(sqlCreateTablaSerie);
        db.execSQL(sqlCreateEpisodio);
        db.execSQL(sqlCreateLink);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {		
	}
	
	public void insertarSerie(SerieExtendida serie) {
		SQLiteDatabase db = getWritableDatabase();
        
		String idSerie, title, poster, thumb, small_thumb, status, synopsis;
		int seasons, episodes, seriesly_score, participants_score, esNueva;
		
		idSerie = serie.getIdSerie();
		title = serie.getTitle();
		poster = serie.getPoster();
		thumb = serie.getThumb();
		small_thumb = serie.getSmall_thumb();
		status = serie.getStatus();
		synopsis = serie.getSynopsis();
		seasons = serie.getSeasons();
		episodes = serie.getEpisodes();
		seriesly_score = serie.getSeriesly_score();
		participants_score = serie.getParticipants_score();
		esNueva = serie.esNueva();
		
        //Si hemos abierto correctamente la base de datos
        if(db != null) {
        	db.execSQL("INSERT INTO Serie (idSerie, title, seasons, episodes, poster, thumb, small_thumb, status, synopsis, " + 
        			"seriesly_score, participants_score, esNueva) VALUES('" + idSerie +  "', '" + title + "', " + seasons + ", " + episodes + 
        			", '" + poster + "', '" + thumb + "', '" + small_thumb + "', '" + status + "', '" + synopsis + "', " +
        			seriesly_score + ", " + participants_score + ", " + esNueva + ")");
        	
        	db.close(); //Cerramos la conexión con la base de datos
        }
	}
	
	public int numSeries() {
		Cursor cursorNumSeries = null;
		int numSeries = 0;
		SQLiteDatabase db = getWritableDatabase();
		
		if(db != null) {
			cursorNumSeries = db.query("Serie", new String[]{"*"}, null, null, null, null, null);
			
			numSeries = cursorNumSeries.getCount();
			
			db.close();
		}
		
		return numSeries;
	}
	
	public SerieExtendida[] obtenerSeries() {
		SerieExtendida[] series = null;
		SQLiteDatabase db = getWritableDatabase();
		
		if(db != null) {
			String []columnas = {"idSerie","title","seasons","episodes","poster","thumb","small_thumb","status","synopsis",
					"seriesly_score", "participants_score", "esNueva"};
			Cursor cursorSeries = db.query("Serie", columnas, null, null, null, null, null);
			
			series = new SerieExtendida[cursorSeries.getCount()];
			
			
			
			for(int i=0; i<cursorSeries.getCount(); i++) {
				cursorSeries.moveToPosition(i);
				
				String idSerie = cursorSeries.getString(0);
				String title = cursorSeries.getString(1);
				int seasons = cursorSeries.getInt(2);
				int episodes = cursorSeries.getInt(3);
				String poster = cursorSeries.getString(4);
				String thumb = cursorSeries.getString(5);
				String small_thumb = cursorSeries.getString(6);
				String status = cursorSeries.getString(7);
				String synopsis = cursorSeries.getString(8);
				int seriesly_score = cursorSeries.getInt(9);
				int participants_score = cursorSeries.getInt(10);
				int esNueva = cursorSeries.getInt(11);
				series[i] = new SerieExtendida(idSerie, title, seasons, episodes, poster, thumb, small_thumb, status, synopsis, seriesly_score,
						participants_score, esNueva);
			}
			
			db.close();
		}
		
		return series;
	}

}